/*
 * TexPool.cpp
 *
 *  Created on: Oct 16, 2012
 *      Author: travis
 */

#ifdef _WIN32 
#include <windows.h> 
#endif

#include "TexPool.h"

// constructor need like input path to
// directory with textures
TexPool::TexPool(std::string _texPath)
{
	texPath = _texPath;
	files = std::vector<texInfo>();
}

TexPool::TexPool()
{

}

TexPool::~TexPool()
{
	// delete textures

	for (std::vector<texInfo>::iterator it = files.begin(); it != files.end();
			++it)
		glDeleteTextures(1, &it->ID);

	files.clear();
}

// get ID of loaded texture
// input - filename
// output - ID of texture in opengl memory
GLuint TexPool::getTexIdByName(std::string filename)
{
	for (std::vector<texInfo>::iterator it = files.begin(); it != files.end();
			++it)
	{
		if (it->fname.compare(filename) == 0)
			return it->ID;
	}

	return 100;
}

// loading immage
// input - filename of image
GLuint TexPool::loadImage(const char* filename)
{
	GLuint num;
	std::string buf;

	buf = texPath;
	buf = buf + filename;

	//std::cout << buf << std::endl;

	SDL_Surface* img = IMG_Load(buf.c_str());

	if (img == NULL)
	{
		std::cerr << "TEXTURES: Image " << buf << " not found" << std::endl;
		return -1;
	}

	SDL_PixelFormat form =
	{ NULL, 32, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0xff000000, 0x00ff0000, 0x0000ff00,
			0x000000ff };

	SDL_Surface * img2 = SDL_ConvertSurface(img, &form, SDL_SWSURFACE);

	if (img2 == NULL)
	{
		std::cerr << "TEXTURES: Cannot convert " << filename << std::endl;
		return -1;
	}

	num = createTex(img2, filename);

	SDL_FreeSurface(img);
	SDL_FreeSurface(img2);

	return num;
}

// create new texture from loaded SDL Surface
// input - SDL Surface with picture
//		 - filename of texture
// output - ID of texture in OGL
// create texture and save it into pool
GLuint TexPool::createTex(SDL_Surface *img, const char* filename)
{
	GLuint num;
	GLuint delImg;
	texInfo tBuff;

	if ((delImg = getTexIdByName(filename)) != (GLuint) 100)
	{
		num = delImg;
	}
	else
	{
		glGenTextures(1, &num);

		tBuff.ID = num;
		tBuff.fname = filename;

#ifdef _DEBUG
		std::cout << "TEXTURES: " << tBuff.fname << "   with ID: " << tBuff.ID << std::endl;
#endif
		files.push_back(tBuff);

		glBindTexture(GL_TEXTURE_2D, num);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, img->w, img->h, 0, GL_RGBA,
				GL_UNSIGNED_INT_8_8_8_8, img->pixels);
	}

	return num;
}

// load whole directory of textures
int TexPool::loadDir()
{
	std::string buf;

	DIR *dp;
	struct dirent *dirp;

	if ((dp = opendir(texPath.c_str())) == NULL)
	{
		std::cerr << "TEXTURES: Error opening " << texPath << std::endl;
		return 1;
	}

	//std::cout << "\nFiles in " << texPath << ": " << std::endl;

	while ((dirp = readdir(dp)) != NULL)
	{
		buf = dirp->d_name;

		if (buf.compare(".") && buf.compare(".."))
		{
			loadImage(dirp->d_name);
		}
	}
	closedir(dp);
	return 0;
}

// return all saved textures information
std::vector<texInfo> TexPool::getTextures()
{
	return files;
}

// return count of loaded textures units
int TexPool::getCount()
{
	return files.size();
}

void TexPool::setTexPath(std::string _texPath)
{
	texPath = _texPath;
}

